import Taro from '@tarojs/taro';

const TARO_ENV = process.env.TARO_ENV;

// 通过wxstorage存储
class ErrorWithWeapp {
    showToast({
        title = '系统异常',
        icon = 'none',
        duration = 3000,
        image = undefined,
        delay = 1000
    }) {
        return setTimeout(function() {
            image
                ? wx.showToast({
                    title, duration, image, icon
                })
                : wx.showToast({
                    title, icon, duration
                });
        }, delay);
    }
    showModal({ title = '提示', content = '' }) {
        return new Promise(resolve => {
            wx.showModal({
                title,
                content,
                success(res) {
                    resolve(Boolean(res.confirm));
                }
            });
        });
    }
}
// 通过localstroage存储
class ErrorWithH5 {
    showToast({ title = '系统异常', icon = 'none', duration = 3000 }) {
        return setTimeout(function() {
            Taro.showToast({
                title: title,
                icon,
                duration
            });
        }, 1000);
    }
}

const adapters = {
    weapp: new ErrorWithWeapp(),
    h5: new ErrorWithH5()
};

const handler = adapters[TARO_ENV];

export default {
    namespace: '$msg',
    state: {},
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *showToast({ payload }) {
            return yield handler.showToast(payload);
        },
        *showModal({ payload }) {
            return handler.showModal(payload);
        }
    }
};
